<%@ page contentType="text/html;charset=UTF-8" %>
<script>
    $(document).ready(function() {
    $('#torderTable').bootstrapTable({

        //请求方法
        method: 'get',
        //类型json
        dataType: "json",
        //显示刷新按钮
        showRefresh: true,
        //显示切换手机试图按钮
        showToggle: true,
        //显示 内容列下拉框
        showColumns: true,
        //显示到处按钮
        showExport: true,
        //显示切换分页按钮
        showPaginationSwitch: true,
        //显示详情按钮
        detailView: true,
        //显示详细内容函数
        detailFormatter: "detailFormatter",
        //最低显示2行
        minimumCountColumns: 2,
        //是否显示行间隔色
        striped: true,
        //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
        cache: false,
        //是否显示分页（*）
        pagination: true,
        //排序方式
        sortOrder: "asc",
        //初始化加载第一页，默认第一页
        pageNumber:1,
        //每页的记录行数（*）
        pageSize: 10,
        //可供选择的每页的行数（*）
        pageList: [10, 25, 50, 100],
        //这个接口需要处理bootstrap table传递的固定参数,并返回特定格式的json数据
        url: "${ctx}/tms/torder/orderDistributionData",
        //默认值为 'limit',传给服务端的参数为：limit, offset, search, sort, order Else
        //queryParamsType:'',
        ////查询参数,每次调用是会带上这个参数，可自定义
        queryParams : function(params) {
            var searchParam = $("#searchForm").serializeJSON();
            searchParam.pageNo = params.limit === undefined? "1" :params.offset/params.limit+1;
            searchParam.pageSize = params.limit === undefined? -1 : params.limit;
            searchParam.orderBy = params.sort === undefined? "" : params.sort+ " "+  params.order;
            return searchParam;
        },
        //分页方式：client客户端分页，server服务端分页（*）
        sidePagination: "server",
        contextMenuTrigger:"right",//pc端 按右键弹出菜单
        contextMenuTriggerMobile:"press",//手机端 弹出菜单，click：单击， press：长按。
        contextMenu: '#context-menu',
        onContextMenuItem: function(row, $el){
            if($el.data("item") == "edit"){
                edit(row.id);
            } else if($el.data("item") == "delete"){
                jp.confirm('确认要删除该订单记录吗？', function(){
                    jp.loading();
                    jp.get("${ctx}/tms/torder/delete?id="+row.id, function(data){
                        if(data.success){
                            $('#torderTable').bootstrapTable('refresh');
                            jp.success(data.msg);
                        }else{
                            jp.error(data.msg);
                        }
                    })

                });

            }
        },

        onClickRow: function(row, $el){
        },
        columns: [{
            checkbox: true

        }
            ,{
                field: 'code',
                title: '订单编号',
                sortable: true
                ,formatter:function(value, row , index){
                    return "<a href='javascript:edit(\""+row.id+"\")'>"+value+"</a>";
                }

            }
            ,{
                field: 'orderDate',
                title: '下单时间',
                sortable: true

            }
            ,{
                field: 'status',
                title: '订单状态',
                sortable: true

            }
            ,{
                field: 'pickDate',
                title: '提货时间',
                sortable: true

            }

            ,{
                field: 'truckType',
                title: '要求车型',
                sortable: true,
                formatter:function(value, row , index){
                    return jp.getDictLabel(${fns:toJson(fns:getDictList('truck_type'))}, value, "-");
                }

            }
            ,{
                field: 'isLoad',
                title: '是否需要装卸工',
                sortable: true,
                formatter:function(value, row , index){
                    return jp.getDictLabel(${fns:toJson(fns:getDictList('is_load'))}, value, "-");
                }

            }
            ,{
                field: 'transportType',
                title: '运输类型',
                sortable: true,
                formatter:function(value, row , index){
                    return jp.getDictLabel(${fns:toJson(fns:getDictList('transport_type'))}, value, "-");
                }

            }
            ,{
                field: 'isReceipt',
                title: '是否需要回单',
                sortable: true,
                formatter:function(value, row , index){
                    return jp.getDictLabel(${fns:toJson(fns:getDictList('is_receipt'))}, value, "-");
                }

            }
            ,{
                field: 'goodsCompany',
                title: '发货单位',
                sortable: true

            }
            ,{
                field: 'goodsContacts',
                title: '发货联系人',
                sortable: true

            }
            ,{
                field: 'goodsPhone',
                title: '发货联系人电话',
                sortable: true

            }
            ,{
                field: 'pickAddress',
                title: '提货地址',
                sortable: true

            }
            ,{
                field: 'pickContacts',
                title: '提货联系人',
                sortable: true

            }
            ,{
                field: 'pickPhone',
                title: '提货联系人电话',
                sortable: true

            }
            ,{
                field: 'miles',
                title: '里程',
                sortable: true

            }
            ,{
                field: 'agencyPayment',
                title: '代收货款',
                sortable: true

            }
            ,{
                field: 'agencyCharge',
                title: '代收货款手续费',
                sortable: true

            }
            ,{
                field: 'receiptAmount',
                title: '回单份数',
                sortable: true

            }
            ,{
                field: 'receiptRequire',
                title: '回单返回要求',
                sortable: true,
                formatter:function(value, row , index){
                    return jp.getDictLabel(${fns:toJson(fns:getDictList('receipt_require'))}, value, "-");
                }

            }
            ,{
                field: 'loadRequire',
                title: '装卸要求',
                sortable: true,
                formatter:function(value, row , index){
                    return jp.getDictLabel(${fns:toJson(fns:getDictList('load_require'))}, value, "-");
                }

            }
            ,{
                field: 'loadCharge',
                title: '装卸费',
                sortable: true

            }
            ,{
                field: 'cart',
                title: '手推车服务',
                sortable: true,
                formatter:function(value, row , index){
                    return jp.getDictLabel(${fns:toJson(fns:getDictList('cart'))}, value, "-");
                }

            }
            ,{
                field: 'transportCharge',
                title: '运输费',
                sortable: true

            }
           /* ,{
                field: 'totalLoadCharge',
                title: '装卸费合计',
                sortable: true

            }*/
            ,{
                field: 'totaCharge',
                title: '费用合计',
                sortable: true

            }
            ,{
                field: 'remarks',
                title: '备注信息',
                sortable: true

            }
        ]

    });


    if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){//如果是移动端


    $('#torderTable').bootstrapTable("toggleView");
}

    $('#torderTable').on('check.bs.table uncheck.bs.table load-success.bs.table ' +
    'check-all.bs.table uncheck-all.bs.table', function () {
    $('#remove').prop('disabled', ! $('#torderTable').bootstrapTable('getSelections').length);
    $('#edit').prop('disabled', $('#torderTable').bootstrapTable('getSelections').length!=1);
});

    $("#btnImport").click(function(){
    jp.open({
    type: 1,
    area: [500, 300],
    title:"导入数据",
    content:$("#importBox").html() ,
    btn: ['下载模板','确定', '关闭'],
    btn1: function(index, layero){
    window.location='${ctx}/tms/torder/import/template';
},
    btn2: function(index, layero){
    var inputForm =top.$("#importForm");
    var top_iframe = top.getActiveTab().attr("name");//获取当前active的tab的iframe
    inputForm.attr("target",top_iframe);//表单提交成功后，从服务器返回的url在当前tab中展示
    inputForm.onsubmit = function(){
    jp.loading('  正在导入，请稍等...');
}
    inputForm.submit();
    jp.close(index);
},

    btn3: function(index){
    jp.close(index);
}
});
});

    $("#search").click("click", function() {// 绑定查询按扭
    $('#torderTable').bootstrapTable('refresh');
});

    $("#reset").click("click", function() {// 绑定查询按扭
    $("#searchForm  input").val("");
    $("#searchForm  select").val("");
    $("#searchForm  .select-item").html("");
    $('#torderTable').bootstrapTable('refresh');
});

    $('#orderDate').datetimepicker({
    format: "YYYY-MM-DD HH:mm:ss"
});

});

    function getIdSelections() {
    return $.map($("#torderTable").bootstrapTable('getSelections'), function (row) {
    return row.id
});
}

    function deleteAll(){

    jp.confirm('确认要删除该订单记录吗？', function(){
        jp.loading();
        jp.get("${ctx}/tms/torder/deleteAll?ids=" + getIdSelections(), function(data){
            if(data.success){
                $('#torderTable').bootstrapTable('refresh');
                jp.success(data.msg);
            }else{
                jp.error(data.msg);
            }
        })

    })
}

    function add(){
    jp.openDialog('新增订单', "${ctx}/tms/torder/distributionForm",'800px', '500px', $('#torderTable'));
}

    function edit(id){//没有权限时，不显示确定按钮
    if(id == undefined){
    id = getIdSelections();
}
    <shiro:hasPermission name="tms:torder:edit">
    jp.openDialog('编辑订单', "${ctx}/tms/torder/distributionForm?id=" + id,'800px', '500px', $('#torderTable'));
    </shiro:hasPermission>
    <shiro:lacksPermission name="tms:torder:edit">
    jp.openDialogView('查看订单', "${ctx}/tms/torder/distributionForm?id=" + id,'800px', '500px', $('#torderTable'));
    </shiro:lacksPermission>
    }





    function detailFormatter(index, row) {
    var htmltpl =  $("#torderChildrenTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
    var html = Mustache.render(htmltpl, {
    idx:row.id
});
    $.get("${ctx}/tms/torder/detail?id="+row.id, function(torder){
    var torderChild1RowIdx = 0, torderChild1Tpl = $("#torderChild1Tpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
    var data1 =  torder.orderAddressList;
    for (var i=0; i<data1.length; i++){
    addRow('#torderChild-'+row.id+'-1-List', torderChild1RowIdx, torderChild1Tpl, data1[i]);
    torderChild1RowIdx = torderChild1RowIdx + 1;
}


})

    return html;
}

    function addRow(list, idx, tpl, row){
    $(list).append(Mustache.render(tpl, {
        idx: idx, delBtn: true, row: row
    }));
}

</script>
<script type="text/template" id="torderChildrenTpl">//<!--
	<div class="tabs-container">
		<ul class="nav nav-tabs">
				<li class="active"><a data-toggle="tab" href="#tab-{{idx}}-1" aria-expanded="true">收货地址</a></li>
		</ul>
		<div class="tab-content">
				 <div id="tab-{{idx}}-1" class="tab-pane fade in active">
						<table class="ani table">
						<thead>
							<tr>
								<th>收货地址</th>
								<th>收货联系人</th>
								<th>收货联系人电话</th>
								<th>备注信息</th>

							</tr>
						</thead>
						<tbody id="torderChild-{{idx}}-1-List">
						</tbody>
					</table>
				</div>
		</div>//-->
</script>
<script type="text/template" id="torderChild1Tpl">//<!--
				<tr>
					<td>
						{{row.receiveAddress}}
					</td>
					<td>
						{{row.receiveContacts}}
					</td>
					<td>
						{{row.receivePhone}}
					</td>
					<td>
						{{row.remarks}}
					</td>

				</tr>//-->
</script>
